package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.CarPageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.AfterSalesMessage;
import com.southwind.entity.Car;
import com.southwind.service.AfterSalesMessageService;
import com.southwind.service.CarService;
import com.southwind.vo.AfterSalesVO;
import com.southwind.vo.CarVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/afterSales")
public class AfterSalesMessageController {

    @Autowired
    private AfterSalesMessageService afterSalesMessageService;
    @Autowired
    private CarService carService;

    @PostMapping("/add")
    public ResultObj add(@RequestBody AfterSalesMessage afterSalesMessage){
        boolean save = this.afterSalesMessageService.save(afterSalesMessage);
        if(save) return ResultObj.AFTERSALES_ADD_SUCCESS;
        return ResultObj.AFTERSALES_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody AfterSalesMessage afterSalesMessage){
        boolean save = this.afterSalesMessageService.updateById(afterSalesMessage);
        if(save) return ResultObj.AFTERSALES_UPDATE_SUCCESS;
        return ResultObj.AFTERSALES_UPDATE_ERROR;
    }

    @GetMapping("/list")
    public Map list(CarPageModel carPageModel){
        Page<AfterSalesMessage> page = new Page<>(carPageModel.getPage(), carPageModel.getLimit());
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(carPageModel.getCarnumber()), "number", carPageModel.getCarnumber());
        List<Car> carList = this.carService.list(queryWrapper);
        List<Integer> idList = new ArrayList<>();
        for (Car car : carList) {
            idList.add(car.getId());
        }
        QueryWrapper<AfterSalesMessage> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in(idList.size() > 0, "cid", idList);
        Page<AfterSalesMessage> resultPage = this.afterSalesMessageService.page(page, queryWrapper1);
        Map map = new HashMap();
        map.put("count", resultPage.getTotal());
        List<AfterSalesVO> list = new ArrayList<>();
        for (AfterSalesMessage record : resultPage.getRecords()) {
            AfterSalesVO afterSalesVO = new AfterSalesVO();
            BeanUtils.copyProperties(record, afterSalesVO);
            afterSalesVO.setNumber(this.carService.getById(record.getCid()).getNumber());
            list.add(afterSalesVO);
        }
        map.put("data", list);
        return map;
    }
}

